//
//  AuthenTypeVC.swift
//  kxf
//
//  Created by Mac on 2024/3/6.
//

import Foundation
import UIKit
import MCToast

class AuthenTypeVC: WHBaseViewVC {
    
    var isFromMine = false
    var merchatChannle = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        sendUserCenterRequest()
        sendChannelListRequest()
        initUI()
    }
    lazy var phoneVm : AuthenTypePhoneVM = {
        let vm = AuthenTypePhoneVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(10), width: 0, height: 0))
        if isFromMine{
            vm.textField.text = UserModel.shared.phone
            vm.textField.isEnabled = false
        }
        return vm
    }()
    lazy var typeVm : AuthenTypeChoiceVM = {
        let vm = AuthenTypeChoiceVM.init(frame: CGRect.init(x: 0, y: self.phoneVm.frame.maxY+kFitWidth(20), width: 0, height: 0))
        
        return vm
    }()
    lazy var choiceChannelAlertVm : ChoiceMercChannelAlertVM = {
        let vm = ChoiceMercChannelAlertVM.init(frame: .zero)
        vm.isHidden = true
        vm.tapBlock = {(channleDict)in
            DLLog(message: "\(channleDict)")
            self.merchatChannle = channleDict["code"]as? String ?? ""
            self.sendRegisterRequest(phone: self.phoneVm.textField.text!)
        }
        return vm
    }()
    lazy var nextBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: self.typeVm.frame.maxY+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("下一步", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
        
        return btn
    }()
}

extension AuthenTypeVC{
    @objc func nextAction() {
        if !judgePhoneNumber(phoneNum: phoneVm.textField.text) && !isFromMine{
            self.presentAlertVcNoAction(title: "请输入正确的手机号", viewController: self)
            return
        }
        if isFromMine{
            if self.typeVm.type == "mini"{//小微
                AuthenMiniMsgModel.shareInstance.tel = phoneVm.textField.text!
                let vc = AuthenMiniVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else if self.typeVm.type == "geti"{//个体
                AuthenCompanyMsgModel.shareInstance.tel = phoneVm.textField.text!
                AuthenCompanyMsgModel.shareInstance.merchantType = "2"
                let vc = AuthenGeTiVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{//企业
                AuthenCompanyMsgModel.shareInstance.tel = phoneVm.textField.text!
                AuthenCompanyMsgModel.shareInstance.merchantType = "0"
                
                let vc = AuthenCompanyVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }else{
            choiceChannelAlertVm.isHidden = false
        }
    }
}
extension AuthenTypeVC{
    func initUI() {
        initNavi(titleStr: "商户进件")
        view.backgroundColor = .COLOR_LIGHT_GREY
        
        if isFromMine{
            view.addSubview(typeVm)
            view.addSubview(nextBtn)
            
            let typeFrame = typeVm.frame
            typeVm.frame = CGRect.init(x: typeFrame.origin.x, y: getNavigationBarHeight()+kFitWidth(10), width: typeFrame.size.width, height: typeFrame.size.height)
        }else{
            view.addSubview(phoneVm)
            view.addSubview(typeVm)
            view.addSubview(nextBtn)
        }
        view.addSubview(choiceChannelAlertVm)
    }
}

extension AuthenTypeVC{
//    func sendGetMsgRequset(tel:String) {
//        let urlString = "\(URL_authen_msg_query)?tel=\(tel)"
//        
//        WHNetworkUtil.shareManager().GET(urlString: urlString,vc:self) { responseObject in
//            DLLog(message: "\(responseObject)")
//            
//            if responseObject["code"]as? Int ?? -1 == 200 {
//                
//            }
//            
//            AuthenMiniMsgModel.shareInstance.refreshMsg(dict: responseObject["data"]as? NSDictionary ?? [:])
//            
//            if self.typeVm.type == "mini"{//小微
//                AuthenMiniMsgModel.shareInstance.tel = tel
//                let vc = AuthenMiniVC()
//                self.navigationController?.pushViewController(vc, animated: true)
//            }else if self.typeVm.type == "geti"{//个体
//                let vc = AuthenGeTiVC()
//                self.navigationController?.pushViewController(vc, animated: true)
//            }else{//企业
//                AuthenCompanyMsgModel.shareInstance.tel = tel
//                let vc = AuthenCompanyVC()
//                self.navigationController?.pushViewController(vc, animated: true)
//            }
//        }
//    }
    func sendRegisterRequest(phone:String){
        MCToast.mc_loading()
        var param:[String:Any] = ["phone":"\(phone)",
                                  "inviteCode":"\(UserModel.shared.inviteCode)",
                                  "merchantType":"",
                                  "channel":"\(self.merchatChannle)",
                                  "tel":"\(phone)"]
        if self.typeVm.type == "mini"{//小微 0   企业   1  小微   2 个体
            AuthenMiniMsgModel.shareInstance.tel = phoneVm.textField.text!
            param.updateValue("1", forKey: "merchantType")
        }else if self.typeVm.type == "geti"{//个体
            AuthenCompanyMsgModel.shareInstance.tel = phoneVm.textField.text!
            param.updateValue("2", forKey: "merchantType")
        }else{//企业
            AuthenCompanyMsgModel.shareInstance.tel = phoneVm.textField.text!
            param.updateValue("0", forKey: "merchantType")
        }
        UserModel.shared.posChannel = merchatChannle
        AuthenCompanyMsgModel.shareInstance.posChannel = merchatChannle
        WHNetworkUtil.shareManager().POST(urlString: URL_register_merc, parameters: param  as [String : AnyObject],isNeedToast: true,vc: self) { responseObject in
            DLLog(message: "\(responseObject)")
            
            if responseObject["code"]as? Int ?? 0 == 200 {
//                self.sendDataSaveRequest(phone: phone)
                if self.typeVm.type == "mini"{//小微
                    let vc = AuthenMiniVC()
                    vc.isAgentEdit = true
                    self.navigationController?.pushViewController(vc, animated: true)
                }else if self.typeVm.type == "geti"{//个体
                    AuthenCompanyMsgModel.shareInstance.merchantType = "2"
                    let vc = AuthenGeTiVC()
                    vc.isAgentEdit = true
                    self.navigationController?.pushViewController(vc, animated: true)
                }else{//企业
                    AuthenCompanyMsgModel.shareInstance.merchantType = "0"
                    
                    if AuthenCompanyMsgModel.shareInstance.posChannel == "fy"{
                        let vc = AuthenCompanyFYVC()
                        vc.isAgentEdit = true
                        self.navigationController?.pushViewController(vc, animated: true)
                    }else{
                        let vc = AuthenCompanyVC()
                        vc.isAgentEdit = true
                        self.navigationController?.pushViewController(vc, animated: true)
                    }
                }
            }else{
                self.presentAlertVcNoAction(title: "\(responseObject["message"]as? String ?? "")", viewController: self)
//                let vc = AuthenPendingVC()
//                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
    
    func sendDataSaveRequest(phone:String){
        MCToast.mc_loading()
        var param:[String:Any] = ["phone":"\(phone)",
                                  "merchantType":"",
                                  "tel":"\(phone)"]
        if self.typeVm.type == "mini"{//小微 0   企业   1  小微   2 个体
            AuthenMiniMsgModel.shareInstance.tel = phoneVm.textField.text!
            param.updateValue("1", forKey: "merchantType")
        }else if self.typeVm.type == "geti"{//个体
            AuthenCompanyMsgModel.shareInstance.tel = phoneVm.textField.text!
            param.updateValue("2", forKey: "merchantType")
        }else{//企业
            AuthenCompanyMsgModel.shareInstance.tel = phoneVm.textField.text!
            param.updateValue("0", forKey: "merchantType")
        }
        WHNetworkUtil.shareManager().POST(urlString: URL_register_merc, parameters: param  as [String : AnyObject],isNeedToast: true,vc: self) { responseObject in
            DLLog(message: "\(responseObject)")
            if self.typeVm.type == "mini"{//小微
                let vc = AuthenMiniVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else if self.typeVm.type == "geti"{//个体
                let vc = AuthenGeTiVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{//企业
                if AuthenCompanyMsgModel.shareInstance.posChannel == "fy"{
                    let vc = AuthenCompanyFYVC()
                    self.navigationController?.pushViewController(vc, animated: true)
                }else{
                    let vc = AuthenCompanyVC()
                    self.navigationController?.pushViewController(vc, animated: true)
                }
            }
        }
    }
    func sendChannelListRequest() {
        WHNetworkUtil.shareManager().POST(urlString: URL_authen_channel_list, parameters: nil) { responseObject in
            DLLog(message: "\(responseObject)")
            let dataArr = responseObject["data"]as? NSArray ?? []
            self.choiceChannelAlertVm.updateUI(array: dataArr)
        }
    }
}
